package com.stylefeng.guns.config;

import com.stylefeng.guns.common.enums.DroolsPathEnum;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.core.io.support.ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX;

/**
 * drools规则引擎配置
 *
 * @author wuchong
 */
@Configuration
public class DroolsAutoConfig {

    @Bean(name = "droolsSessionMap")
    public Map<String, KieSession> getMapKieSession() throws IOException {

        Map<String, KieSession> kieSessionMap = new HashMap<>();

        for (DroolsPathEnum path : DroolsPathEnum.values()) {

            KieServices kieServices = KieServices.Factory.get();

            KieFileSystem kieFileSystem = kieServices.newKieFileSystem();

            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

            Resource[] resource = resourcePatternResolver.getResources("classpath*:" + path.getRulesPath() + "**/*.*");

            for (Resource file : resource) {

                kieFileSystem.write(ResourceFactory.newClassPathResource(path.getRulesPath() + file.getFilename(), "UTF-8"));
            }

            final KieRepository kieRepository = kieServices.getRepository();

            kieRepository.addKieModule(() -> kieRepository.getDefaultReleaseId());

            KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);

            kieBuilder.buildAll();

            KieContainer kieContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());

            KieSession kieSession = kieContainer.newKieSession();

            kieSessionMap.put(path.getName(), kieSession);

        }

        return kieSessionMap;

    }


//    /**
//     * 规则文件目录
//     */
//    private static final String RULES_PATH = "rules/";
//
//    @Bean
//    @ConditionalOnMissingBean(KieFileSystem.class)
//    public KieFileSystem kieFileSystem() throws IOException {
//        // 文件系统（内存中）
//        final KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
//        for (Resource file : getRuleFiles()) {
//            kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
//        }
//        return kieFileSystem;
//
//    }
//
//    @Bean
//    @ConditionalOnMissingBean(KieContainer.class)
//    public KieContainer kieContainer() throws IOException {
//        final KieRepository kieRepository = getKieServices().getRepository();
//
//        kieRepository.addKieModule(new KieModule() {
//
//            public ReleaseId getReleaseId() {
//                return kieRepository.getDefaultReleaseId();
//            }
//        });
//
//
//        KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
//        kieBuilder.buildAll();
//
//        return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
//    }
//
//
//
//
////    @Bean
////    @ConditionalOnMissingBean(KieBase.class)
////    public KieBase kieBase() throws IOException {
////        return kieContainer().getKieBase();
////    }
////
////    @Bean
////    @ConditionalOnMissingBean(KieSession.class)
////    public KieSession kieSession() throws IOException {
////        return kieContainer().newKieSession();
////    }
//
//    @Bean
//    @ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)
//    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
//        return new KModuleBeanFactoryPostProcessor();
//    }
//
//    /**
//     * 规则文件资源
//     *
//     * @return
//     * @throws IOException
//     */
//    private Resource[] getRuleFiles() throws IOException {
//        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//        return resourcePatternResolver.getResources(CLASSPATH_ALL_URL_PREFIX + RULES_PATH + "**/*.*");
//    }
//
//    private KieServices getKieServices() {
//        return KieServices.Factory.get();
//    }

}
